﻿Imports System.Data.OleDb
Imports BUS
Imports DTO

Public Class frmQuanLyXE
    Dim dtXE As DataTable
    'Dim adapterXE As OleDbDataAdapter
    Dim vtClick As Integer = 0


    Private Sub QuanLyXE_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Dim kt As String = XL_Chung.KiemTraKetNoi_CSDL_CoDuocKhong()
        'If kt <> "" Then
        '    MessageBox.Show(kt)
        'Else
        dgvXe.DataSource = XeBus.LayDanhSachXe()

        ' Try
        Dim xe As XeDto = LayXeTu_DataGridView(0)
        HienThiThongTinXE(xe)
        ResetTextboxs()
        'Catch ex As Exception
        ' End Try
        'End If
    End Sub
    ''' <summary>
    ''' '
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function KT_NhapLieu_Them() As String
        Dim str As String = ""
        If txtMaXe.Text <> "" AndAlso Not IsNumeric(txtMaXe.Text) Then
            str = "Mã xe là số!!!"
            txtMaXe.Focus()
        ElseIf txtSoXe.Text = "" Then
            str = "Số xe không được rỗng!!!"
            txtSoXe.Focus()
        ElseIf txtHieuXe.Text = "" Then
            str = "Hiệu xe không được rỗng!!!"
            txtHieuXe.Focus()
        ElseIf txtDoiXe.Text = "" Or Not IsNumeric(txtDoiXe.Text) Then
            str = "Đời xe không rỗng và phải là số!!!"
            txtDoiXe.Focus()
        ElseIf txtSoChoNgoi.Text = "" Or Not IsNumeric(txtSoChoNgoi.Text) Then
            str = "Số chỗ ngồi không rỗng và phải là số!!!"
            txtSoChoNgoi.Focus()
        End If
        Return str
    End Function

    Public Function LayXeTuTextboxs() As XeDto
        Dim xe As New XeDto
        If IsNumeric(txtMaXe.Text) Then
            xe.MaXe = txtMaXe.Text
        End If
        xe.SoXe = txtSoXe.Text
        xe.HieuXe = txtHieuXe.Text

        If IsNumeric(txtDoiXe.Text) Then
            xe.DoiXe = txtDoiXe.Text
        End If

        If IsNumeric(txtSoChoNgoi.Text) Then
            xe.SoChoNgoi = txtSoChoNgoi.Text
        End If

        Return xe
    End Function

    Public Function LayXeTu_DataGridView(ByVal vtdong As Integer) As XeDto
        Dim xe As New XeDto
        Try
            xe.MaXe = dgvXe.Rows(vtdong).Cells("MaXe").Value
            xe.SoXe = dgvXe.Rows(vtdong).Cells("SoXe").Value
            xe.HieuXe = dgvXe.Rows(vtdong).Cells("HieuXe").Value
            xe.DoiXe = dgvXe.Rows(vtdong).Cells("DoiXe").Value
            xe.SoChoNgoi = dgvXe.Rows(vtdong).Cells("SoChoNgoi").Value
        Catch ex As Exception
        End Try

        Return xe
    End Function

    Public Sub HienThiThongTinXE(ByVal xe As XeDto)
        txtMaXe.Text = xe.MaXe
        txtSoXe.Text = xe.SoXe
        txtHieuXe.Text = xe.HieuXe
        txtDoiXe.Text = xe.DoiXe
        txtSoChoNgoi.Text = xe.SoChoNgoi
    End Sub

    Public Sub ResetTextboxs()
        txtMaXe.Text = ""
        txtSoXe.Text = ""
        txtHieuXe.Text = ""
        txtDoiXe.Text = ""
        txtSoChoNgoi.Text = ""
    End Sub

    Private Sub btnTimXe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimXe.Click
        dgvXe.DataSource = XeBus.TimXe(Me.LayXeTuTextboxs())
    End Sub

    Private Sub btnThemXe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThemXe.Click
        ResetTextboxs()
        btnThemXe.Visible = False
        btnCapNhatThem.Visible = True
        btnThemXe.Visible = False
        btnHuy.Enabled = True
        btnCapNhatXe.Enabled = False
        btnXoaXe.Enabled = False
    End Sub

    Private Sub btnHuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        btnThemXe.Visible = True
        btnCapNhatThem.Visible = False
        btnHuy.Enabled = False
        btnCapNhatXe.Enabled = True
        btnXoaXe.Enabled = True
        dgvXe.DataSource = XeBus.LayDanhSachXe()

        Try
            Dim xe As XeDto = LayXeTu_DataGridView(0)
            HienThiThongTinXE(xe)
        Catch ex As Exception
        End Try
    End Sub

    Private Sub btnCapNhatThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCapNhatThem.Click
        Dim ktNhapLieu As String = KT_NhapLieu_Them()
        If (ktNhapLieu <> "") Then
            MessageBox.Show(ktNhapLieu, "THÔNG BÁO")
            Exit Sub
        End If

        Dim kq As Integer = XeBus.ThemXe(Me.LayXeTuTextboxs())
        If (kq = 0) Then
            MessageBox.Show("Số xe đã có trong dữ liệu!!!", "THÔNG BÁO")
        Else
            dgvXe.DataSource = XeBus.LayDanhSachXe()

            dgvXe.Rows(0).Selected = False
            dgvXe.Rows(dgvXe.Rows.Count - 2).Selected = True
            Dim xe As XeDto = LayXeTu_DataGridView(dgvXe.Rows.Count - 2)
            HienThiThongTinXE(xe)
        End If
    End Sub


    Private Sub btnXoaXe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoaXe.Click

        If vtClick = dgvXe.Rows.Count - 1 Then
            MessageBox.Show("Chọn dòng có dữ liệu cần xóa!!!", "THÔNG BÁO")
            Exit Sub
        End If
        Dim ma As Integer = dgvXe.Rows(vtClick).Cells("MaXE").Value

        Dim kq As Integer = XeBus.XoaXe(ma)
        dgvXe.DataSource = XeBus.LayDanhSachXe()

        Dim xe As XeDto = LayXeTu_DataGridView(0)
        HienThiThongTinXE(xe)
    End Sub

    Private Sub btnCapNhatXe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCapNhatXe.Click
        Dim ktNhapLieu As String = KT_NhapLieu_Them()
        If (ktNhapLieu <> "") Then
            MessageBox.Show(ktNhapLieu, "THÔNG BÁO")
            Exit Sub
        End If

        Dim kq As Integer = XeBus.CapNhatXe(Me.LayXeTuTextboxs())
        If (kq = 0) Then
            MessageBox.Show("Dữ liệu không thay đổi!!!", "THÔNG BÁO")
        Else
            dgvXe.DataSource = XeBus.LayDanhSachXe()

            dgvXe.Rows(0).Selected = False
            dgvXe.Rows(vtClick).Selected = True
            Dim xe As XeDto = LayXeTu_DataGridView(vtClick)
            HienThiThongTinXE(xe)
        End If
    End Sub

    Private Sub dgvXe_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgvXe.MouseClick

        '  Try
        vtClick = dgvXe.CurrentRow.Index()
        If vtClick = dgvXe.Rows.Count - 1 Then
            ResetTextboxs()
            Exit Sub
        End If
        Dim xe As XeDto = LayXeTu_DataGridView(vtClick)
        HienThiThongTinXE(xe)
        ' Catch ex As Exception

        ' End Try

    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Close()
    End Sub

    Private Sub Label5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label5.Click

    End Sub
End Class
